﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace MicroDal
{
    public class TransactionalScope : IDisposable
    {
        private DbTransaction _tx;
        private bool _committed = false;
        private bool _rolledback = false;

        internal TransactionalScope(DataSession session, IsolationLevel isolationLevel)
        {
            _tx = session.CurrentResource.GetTransaction(isolationLevel);
        }

        public void Commit()
        {
            _tx.Commit();
            _committed = true;
        }

        public void Rollback()
        {
            _tx.Rollback();
            this._rolledback = true;
        }

        public void Dispose()
        {
            if (_tx != null)
            {
                if (!_committed)
                {
                    this.Rollback();
                }
                _tx.Dispose();
            }
        }

        public bool IsOpen {
            get { return !this._committed && !this._rolledback; }
        }
    }
}
